class Solution
{
public:
    long long continuousSubarrays(vector<int> &nums)
    {
        long long result = 0;
        multiset<int> s;
        int n = nums.size();
        int start = 0;
        int end = 0;

        while (end < n)
        {
            s.insert(nums[end]);
            while (*s.rbegin() - *s.begin() > 2)
            {
                s.erase(s.find(nums[start]));
                ++start;
            }
            long long len = end - start + 1;
            result += len;
            ++end;
        }
        return result;
    }
};